package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dao.UmsMemberDao;
import com.macro.mall.dto.UmsMemberParam;
import com.macro.mall.dto.UmsMemberQueryParam;
import com.macro.mall.mapper.UmsMemberMapper;
import com.macro.mall.model.UmsMember;
import com.macro.mall.model.UmsMemberExample;
import com.macro.mall.service.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import java.util.Date;
import java.util.List;


/**
 * 会员管理Service实现类
 * Created by P7 on 2021/4/11.
 */
@Service
public class UmsMemberServiceImpl implements UmsMemberService {

    @Autowired
    private UmsMemberDao memberDao;
    @Autowired
    private UmsMemberMapper memberMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public List<UmsMember> list(UmsMemberQueryParam queryParam, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<UmsMember> m = memberDao.getList(queryParam);
        return m;
    }

    @Override
    public int updateMemberStatus(List<Long> ids, Integer status) {
        UmsMember record = new UmsMember();
        record.setStatus(status);
        UmsMemberExample example = new UmsMemberExample();
        example.createCriteria().andIdIn(ids);
        return memberMapper.updateByExampleSelective(record, example);
    }

    @Override
    public UmsMember register(UmsMemberParam umsMemberParam) {
        UmsMember umsMember = new UmsMember();
        BeanUtils.copyProperties(umsMemberParam, umsMember);
        umsMember.setCreateTime(new Date());
        //查询是否有相同用户名的用户
        UmsMemberExample example = new UmsMemberExample();
        example.createCriteria().andUsernameEqualTo(umsMember.getUsername());
        List<UmsMember> umsMemberList = memberMapper.selectByExample(example);
        if (umsMemberList.size() > 0) {
            return null;
        }
        //将密码进行加密操作
        String encodePassword = passwordEncoder.encode(umsMember.getPassword());
        umsMember.setPassword(encodePassword);
        memberMapper.insert(umsMember);
        return umsMember;
    }
}
